package com.borrow.jiemihua.mainUI.activity.LoginRegister;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.borrow.jiemihua.R;
import com.borrow.jiemihua.base.BaseActivity;
import com.borrow.jiemihua.base.BaseApplication;
import com.borrow.jiemihua.base.BaseConstant;
import com.borrow.jiemihua.base.URLConstant;
import com.borrow.jiemihua.mainUI.MainActivity;
import com.borrow.jiemihua.utils.ConfirmUtils;
import com.borrow.jiemihua.utils.CountDownTimerUtils;
import com.borrow.jiemihua.utils.SharedPreferencesUtils;
import com.borrow.jiemihua.utils.xutils3.MyCallBack;
import com.borrow.jiemihua.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import cc.duduhuo.custoast.CusToast;

@ContentView(R.layout.activity_forget_password)
public class ForgetPasswordActivity extends BaseActivity {
    @ViewInject(R.id.tv_top_title)
    private TextView tv_top_title;
    @ViewInject(R.id.et_code)
    private EditText et_code;
    @ViewInject(R.id.et_phone)
    private EditText et_phone;
    @ViewInject(R.id.tv_gainCode)
    private TextView tv_gainCode;
    @ViewInject(R.id.et_pass)
    private EditText et_pass;
    private String phone;
    private String smsCode,password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        initData();
        initView();
    }

    @Override
    protected void initData() {
        tv_top_title.setText("重设密码");

    }

    @Override
    protected void initView() {
        et_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.length()==6){
                    if((!ConfirmUtils.isMobileNO(et_phone.getText().toString()))) {
                        CusToast.showToast("请输入正确的手机号");
                        return;
                    }else{
                        upXIAOJIANcord(et_code.getText().toString());
                    }
                }
            }
        });
    }
    @Event(value = {R.id.imgbtn_left, R.id.tv_gainCode, R.id.tv_login}, type = View.OnClickListener.class)
    private void getEvent(View view) {
        switch (view.getId()) {
            case R.id.imgbtn_left:
                finish();
                break;
            case R.id.tv_gainCode:
                phone = et_phone.getText().toString();
                if (!ConfirmUtils.isMobileNO(phone)) {
                    Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                } else {
                    upLoadRecord(phone);
                }
                break;
            case R.id.tv_login:
                phone = et_phone.getText().toString().trim();
                smsCode = et_code.getText().toString().trim();
                password = et_pass.getText().toString().trim();
                 if (!ConfirmUtils.isMobileNO(phone)) {
                    CusToast.showToast("请输入正确的手机号");
                    return;
                }else if ("".equals(smsCode)||smsCode.length()<6) {
                    CusToast.showToast("请输入正确的验证码");
                    return;
                }else if (password.length() < 6) {
                    CusToast.showToast("请输入不少于6位数的密码");
                    return;
                } else if (ConfirmUtils.isMobileNO(phone) && password.length() >= 6) {
                    upforgetPass();
                }
                break;
            default:
                break;
        }

    }
    private void upXIAOJIANcord(String smsCode) {
        Map<String, String> map = new HashMap<>();
        map.put("cellphone", et_phone.getText().toString());
        map.put("smsCode", smsCode);
        XUtil.Get(URLConstant.XIAOJIANMAHUOQU, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("==校检验证码--", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String msg = jsonObject.optString("msg");
                    if ("1".equals(jsonObject.optString("isSuccess"))) {
                        et_code.setTextColor(getResources().getColor(R.color.black_1));
                    }else {
                        et_code.setTextColor(Color.RED);
                        CusToast.showToast(msg);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                ex.printStackTrace();
            }
        });
    }
    private void upforgetPass(){
        Map<String,Object> map=new HashMap<>();

        map.put("cellphone",phone);
        map.put("smsCode",smsCode);
        map.put("password",password);
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("--忘记密码参数--" + key, "" + value + "\n");
            }
        }
        showDialog();
        XUtil.Post(URLConstant.FORGET_PASS,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("==忘记密码--",result);
                try {
                    JSONObject jsonObject=new JSONObject(result);
                    if("1".equals(jsonObject.optString("isSuccess"))){
                        BaseApplication.destoryActivity("login");
                        CusToast.showToast("密码重置成功");
                        finish();
                    }else{
                        CusToast.showToast(jsonObject.optString("msg"));
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });
    }
    private void upLoadRecord(String inputPhone) {
        Map<String, String> map = new HashMap<>();
        map.put("cellphone", phone);
        showDialog();
        XUtil.Get(URLConstant.YANZHENGMAHUOQU, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("==忘记获取验证码--", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String msg = jsonObject.optString("msg");
                    if ("1".equals(jsonObject.optString("isSuccess"))) {
                        CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(tv_gainCode, 60000, 1000);
                        mCountDownTimerUtils.start();
                        et_code.setTextColor(getResources().getColor(R.color.black_1));
                        et_code.setHint("请输入验证码");
                        et_code.setHintTextColor(getResources().getColor(R.color.text_9));
                    }
                    CusToast.showToast(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });
    }
}
